<?php

ini_set("display_errors",true);
error_reporting(E_ALL ^ E_NOTICE);

include dirname(__FILE__) . '/lib/securimage/securimage.php';

if (!defined('SI_IMAGE_BMP'))
  define('SI_IMAGE_BMP',  100);

class MySecurimage extends Securimage {

	// dir where to store the image
	var $fname = "";	
	
	function MySecurimage() {
		parent::Securimage();	
	}

	// override these methods because we want to maintain case senstivity
	function saveData()
	{
		$this->answer  = $this->code;
	}

	function getCode()
    	{
		return $this->answer;
    	}

	// override this method because we also want to output to file
	function doImage()
	{
		if ($this->use_gd_font == true) {
		    $this->iscale = 1;
		}
		if($this->use_transparent_text == true || $this->bgimg != "") {
		    $this->im     = imagecreatetruecolor($this->image_width, $this->image_height);
		    $this->tmpimg = imagecreatetruecolor($this->image_width * $this->iscale, $this->image_height * $this->iscale);

		} else { //no transparency
		    $this->im     = imagecreate($this->image_width, $this->image_height);
		    $this->tmpimg = imagecreate($this->image_width * $this->iscale, $this->image_height * $this->iscale);
		}

		$this->allocateColors();
		imagepalettecopy($this->tmpimg, $this->im);

		$this->setBackground();

		$this->createCode();

		if (!$this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();

		$this->drawWord();
		if ($this->use_gd_font == false && is_readable($this->ttf_file)) $this->distortedCopy();

		if ($this->draw_lines_over_text && $this->num_lines > 0) $this->drawLines();

		if (trim($this->image_signature) != '')    $this->addSignature();

		// output image
		$this->output();

	}

	// override this method because we also to store the image
	function output()
	{
		if($this->show)
		{
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
			header("Cache-Control: no-store, no-cache, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0", false);
			header("Pragma: no-cache");
		}

		switch($this->image_type)
		{
		    case SI_IMAGE_JPEG:
			if($this->show) 
			{			
				header("Content-Type: image/jpeg");
			}
			
			imagejpeg($this->im, $this->fname, 90);

			if($this->show) 
			{
				imagejpeg($this->im, null, 90);
			}
			
			break;


		    case SI_IMAGE_GIF:
			if($this->show) 
			{			
				header("Content-Type: image/gif");
			}
			
			imagegif($this->im,$this->fname);
			
			
			if($this->show) 
			{
				imagegif($this->im);
			}
			
			break;

		    default:
			if($this->show) 
			{			
				header("Content-Type: image/png");
			}
			
			imagepng($this->im,$this->fname);

			if($this->show) 
			{
				imagepng($this->im);
			}
			
			break;
		}

		//chmod($this->fname,0744);
		imagedestroy($this->im);
		exit;
	}


	/*
	Call this function to generate captcha image.
	Image settings must be supplied in an associative array.
	Array's keys are the parameters we want to provide. 
	Supported parameters are:
	
	- fname: filename for this image
	- char: list of allowed characters to be displayed
	- level: perturbation/distortion level, min=0, max=1
	- width: image width
	- height: image height
	- length: number of chars in the image
	- lines: number of lines
	- textR: R component of text color
	- textG: G component of text color
	- textB: B component of text color
	- lineR: R component of line color
	- lineG: G component of line color
	- minAngle: minimum angle of text rotation
	- maxAngle: maximum angle of text rotation
	- show: image to browser
		
	*/
	function generateCaptcha($settings,$show=false)
	{
		// build settings url from supplied settings values
		$settings_url = "";	
		$countParams = 1;
		foreach($settings as $param=>$value) 
		{
			$settings_url .= $param . "=" . $value; 
		
			$countParams++;
			if($countParams <= count($settings))
			{
				$settings_url .= "&";
			}		

		
		}

		$imgUrl = "mysecurimage_show.php?" . $settings_url;

		if(!$show) 
		{	
			$style = 'style="display:none"';
		}

		echo '<img src="' . $imgUrl . '" ' . $style . ' /><br/>';
	}

	
	
}

?>
